home *** CD-ROM | disk | FTP | other *** search
- // icon.cpp
-
- // includes
- #define PILOT_PRECOMPILED_HEADERS_OFF
- #include <Pilot.h>
- #include "app_constants.h"
- #include "icon.h"
- #include "drawing.h"
- #include "events.h"
-
- // constants
- const int name_margins = 2;
-
- //
- // constructor
- //
- icon::icon(int in_bitmapID, int in_maskID, const char* in_name, int x, int y, view* in_superview):
- pane(in_superview), commander()
- {
- m_bitmapID = in_bitmapID;
- m_maskID = in_maskID;
- StrCopy (m_name, in_name);
- m_x = x;
- m_y = y;
- m_selected = false;
-
- recalculate_rect();
- }
-
- //
- // destructor
- //
- icon::~icon() {
- }
-
- //
- // get_position
- //
- void
- icon::get_position (int& x, int& y) {
- x = m_x;
- y = m_y;
- }
-
- //
- // select
- //
- void
- icon::select() {
- m_selected = true;
- draw_self();
- }
-
- //
- // deselect
- //
- void
- icon::deselect() {
- m_selected = false;
- draw_self();
- }
-
- void
- icon::set_icon (int in_bitmapID, int in_maskID, Boolean in_redraw) {
- m_bitmapID = in_bitmapID;
- m_maskID = in_maskID;
- if (in_redraw)
- draw_self();
- }
-
- //
- // do_command
- //
- Boolean
- icon::do_cmd_self (int in_eventID, void* io_data) {
- Boolean handled = false;
- icon* iconP = (icon*) io_data;
-
- switch (in_eventID) {
- case evt_deselect_icons:
- if (iconP != this)
- deselect();
- break;
- default:
- break;
- }
-
- return handled;
- }
-
- #pragma mark -
-
- //
- // get_name_rect
- //
- void
- icon::get_name_rect ( RectangleType* r ) {
- unsigned short length = StrLen(m_name);
- int width = FntCharsWidth(m_name, length) + name_margins*2;
- int height = FntLineHeight();
- int top = m_y;
- int left = m_x - width/2;
-
- r->topLeft.x = left;
- r->topLeft.y = top;
- r->extent.x = width;
- r->extent.y = height;
- }
-
- //
- // get_bitmap_rect
- //
- void
- icon::get_bitmap_rect ( RectangleType* r ) {
- int height, width;
-
- get_bitmap_dimensions(m_bitmapID, height, width);
- r->topLeft.x = m_x - width/2;
- r->topLeft.y = m_y - height;
- r->extent.x = width;
- r->extent.y = height;
- }
-
- //
- // recalculate_rect()
- //
- void
- icon::recalculate_rect() {
- RectangleType name_r;
- int bitmap_height, bitmap_width;
-
- get_name_rect(&name_r);
- get_bitmap_dimensions(m_bitmapID, bitmap_height, bitmap_width);
-
- int top = m_y - bitmap_height;
- int height = bitmap_height + name_r.extent.y;
-
- int width;
- if (name_r.extent.x > bitmap_width) {
- // use name's width
- width = name_r.extent.x;
- } else {
- // use bitmap's width
- width = bitmap_width;
- }
-
- int left = m_x - width/2;
-
- m_bounds.topLeft.x = left;
- m_bounds.topLeft.y = top;
- m_bounds.extent.x = width;
- m_bounds.extent.y = height;
- }
-
- //
- // draw_self()
- //
- void
- icon::draw_self() {
- RectangleType bitmap_r;
- RectangleType name_r;
-
- // draw icon
- get_bitmap_rect(&bitmap_r);
- draw_bitmap_masked (m_bitmapID, m_maskID, m_x, m_y, center_align, bottom_align);
-
- // draw name
- get_name_rect(&name_r);
- WinEraseRectangle(&name_r, 0);
- FntSetFont (stdFont);
- WinSetUnderlineMode (noUnderline);
- draw_string (m_name, m_x, m_y, center_align, top_align);
-
- // draw hilite
- if (m_selected) {
- draw_bitmap(m_maskID, m_x, m_y, center_align, bottom_align, scrXOR);
- WinInvertRectangle(&name_r, 0);
- }
- }
-
-
- //
- // click_self()
- //
- Boolean
- icon::click_self(int x, int y) {
- Boolean handled = false;
- Boolean pen_down;
- short new_x, new_y, d_x, d_y;
- short old_x = x;
- short old_y = y;
- WinHandle old_bits = NULL;
- unsigned short err;
-
- // check icon click
- RectangleType r;
- get_bitmap_rect(&r);
- if (RctPtInRectangle(x, y, &r))
- handled = true;
-
- // check text click
- get_name_rect(&r);
- if (RctPtInRectangle(x, y, &r))
- handled = true;
-
- // if handled, move icon
- if (handled == true) {
- // should deselect other icons
- commander::dispatch_command (evt_deselect_icons, this);
- select();
- EvtGetPen ( &new_x, &new_y, &pen_down );
- while (pen_down==true) {
- if ( (new_x != old_x) || (new_y != old_y) ) {
- // constrain to screen
- constrain_to_screen (new_x, new_y);
-
- d_x = new_x - old_x;
- d_y = new_y - old_y;
- old_x = new_x;
- old_y = new_y;
-
- m_x = m_x + d_x;
- m_y = m_y + d_y;
-
- if (old_bits!=NULL)
- WinRestoreBits(old_bits, m_bounds.topLeft.x, m_bounds.topLeft.y);
- recalculate_rect();
- old_bits = WinSaveBits(&m_bounds, &err);
- draw_self();
- }
- EvtGetPen ( &new_x, &new_y, &pen_down );
- }
- if (old_bits!=NULL)
- WinRestoreBits(old_bits, m_bounds.topLeft.x, m_bounds.topLeft.y);
- send_update_event();
- }
-
- // return false so other icons can deselect?
- return handled;
- }
-